package com.oocl.dao.impl;

import java.util.ArrayList;
import java.util.List;

import oracle.net.aso.s;

import org.springframework.stereotype.Repository;

import com.oocl.dao.OrderCommentDao;
import com.oocl.dao.base.BaseJPADaoUtil;
import com.oocl.entity.OrderComment;
import com.oocl.entity.Orders;
import com.oocl.util.VoUtil;
import com.oocl.vo.OrderCommentVo;
import com.oocl.vo.OrderCommentVo1;


@Repository("orderCommentDao")
public class OrderCommentDaoImpl extends BaseJPADaoUtil implements OrderCommentDao{

	
	/*
     * @Method  findByCid
     * @function load OrderComments by   cid
     * @param  cid Clients id 
     * @return list OrderComment List
     * @author garen
     * @date 2017-8-12
     * */
	public List<OrderCommentVo1> findByCid(String cid) {
		String sql = "select oc from OrderComment oc left join fetch oc.merchant m left join fetch oc.client c where oc.cid=:cid ";
		List<OrderComment> list =  entityManager.createQuery(sql)
		                       .setParameter("cid", cid)
		                       .getResultList();
		List<OrderCommentVo1> listvo = new ArrayList<OrderCommentVo1>();
		for(OrderComment oc: list){
			String sql1 = "select o from Orders o where o.oid=:oid";
			List<Orders> listo = entityManager.createQuery(sql1)
             .setParameter("oid", oc.getOid())
             .getResultList();
			OrderCommentVo1 ocVo = new OrderCommentVo1();
			try {
				oc.setOid(null);
				ocVo = (OrderCommentVo1) VoUtil.copyWithoutNull(oc, ocVo);
				listvo.add(ocVo);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return listvo;
	}
	/*
     * @Method  findByMerchant
     * @function load OrderComments by   Icnum
     * @param  icnum Merchant Icnum 
     * @return list OrderComment List
     * @author lionel
     * @date 2017-8-13
     * */

	public List<OrderCommentVo1> findByMerchant(String icnum) {
		String sql = "select o from OrderComment o left join fetch o.merchant m left join fetch o.client c where m.icnum=:icnum ";
		List<OrderComment> list =  entityManager.createQuery(sql)
		                       .setParameter("icnum", icnum)
		                       .getResultList();
		
		List<OrderCommentVo1> listvo = new ArrayList<OrderCommentVo1>();
		for(OrderComment oc: list){
			System.out.println(oc.getOid());
			String sql1 = "select o from Orders o where o.oid=:oid";
			List<Orders> listo = entityManager.createQuery(sql1)
             .setParameter("oid", oc.getOid())
             .getResultList();
			OrderCommentVo1 ocVo = new OrderCommentVo1();
			try {
				oc.setOid(null);
				ocVo = (OrderCommentVo1) VoUtil.copyWithoutNull(oc, ocVo);
				System.out.println(listo.get(0).getOid());
				listvo.add(ocVo);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return listvo;
	}

	/*
     * @Method  findByoid
     * @function load OrderComments by   oid
     * @param  oid orders oid 
     * @return list OrderComment List
     * @author lionel
     * @date 2017-8-15
     * */
	public List<OrderCommentVo1> findByoid(String oid) {
		String sql = "select o from OrderComment o  where o.oid=:oid";
		List<OrderComment> list =  entityManager.createQuery(sql)
		                       .setParameter("oid", oid)
		                       .getResultList();
		List<OrderCommentVo1> listvo = new ArrayList<OrderCommentVo1>();
		for(OrderComment oc: list){
			String sql1 = "select o from Orders o where o.oid=:oid";
			List<Orders> listo = entityManager.createQuery(sql1)
             .setParameter("oid", oc.getOid())
             .getResultList();
			OrderCommentVo1 ocVo = new OrderCommentVo1();
			try {
				oc.setOid(null);
				ocVo = (OrderCommentVo1) VoUtil.copyWithoutNull(oc, ocVo);
				listvo.add(ocVo);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return listvo;
	}

	@Override
	public OrderCommentVo1 findById(String id) {
       	OrderComment orderComment =  entityManager.find(OrderComment.class, id);
       	String sql1 = "select o from Orders o where o.oid=:oid";
		List<Orders> listo = entityManager.createQuery(sql1)
         .setParameter("oid", orderComment.getOid())
         .getResultList();
		OrderCommentVo1 ocVo = new OrderCommentVo1();
		try {
			orderComment.setOid(null);
			ocVo = (OrderCommentVo1) VoUtil.copyWithoutNull(orderComment, ocVo);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ocVo;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<OrderComment> getByMerchantIcnum(String icum) {
		String jpql = "select o from OrderComment o where o.merchant.icnum =:icum";
		return (List<OrderComment>) this.entityManager.createQuery(jpql).setParameter("icum", icum).getResultList();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<String> getOrderIdByIcum(String icum) {
		String jpqlString = "select distinct o.oid from OrOrderComment o where o.icum =: icum";
		return this.entityManager.createQuery(jpqlString).setParameter("icum", icum).getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<OrderComment> getByMerchantIcnumAndStatus(String icum,
			String status) {
		String jpqlString = "select o from OrderComment o where o.merchant.icnum =:icum and o.status =:status";
		return this.entityManager.createQuery(jpqlString).setParameter("icum", icum)
				.setParameter("status", status).getResultList();
	}


}
